package it.unibo.iss.domotica;

import it.unibo.iss.domotica.consumptionControlling.ConsumptionControllerFacade;
import it.unibo.iss.domotica.consumptionControlling.IConsumptionController;
import it.unibo.iss.domotica.edControlling.EDController;
import it.unibo.iss.domotica.edDomain.*;
import it.unibo.iss.domotica.readingData.Receiver;
import it.unibo.iss.domoticaContact.DomoticaContact;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 * @(#) SysDomotich.java
 */

public class SysDomotichScontrol extends DomoticaContact {
	private IConsumptionController consumptionController;
	private IImpiantoElettrico elettrodomestici;
//	private EDController edController;
//	private Receiver receiver;
	private String propertiesPath = "G:\\Documenti\\Universita\\Magistrale\\Ingegneria dei Sistemi Software M\\workspace\\domotica.scontrol\\src\\system.properties";

	// Logger
	private static final Logger logger = Logger.getLogger(SysDomotichScontrol.class);

	@Override
	protected void configure() {
		logger.info("--------- Start configuration ---------");
		configureTheSystem();
		super.configure();
		logger.info("--------- End configuration ---------");
	}
	
	public void configureTheSystem() {

		//*** System properties ***//
		logger.info("Configuring properties...");
		
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(propertiesPath));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		//*** ConsumptionController e Receiver ***//
		logger.info("Configuring consumption controller...");
		consumptionController = new ConsumptionControllerFacade(Double.valueOf(prop.getProperty("consumoComplMax")));

		//*** EDController ***//
		logger.info("Configuring ED controller...");
		try {
			edController = new EDController(consumptionController);
//			edController.setName("edController");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//*** Receiver ***//
		logger.info("Configuring receiver...");
		try {
			receiver = new Receiver(ImpiantoElettrico.getInstance());
//			receiver.setName("receiver");
		} catch (Exception e) {
			e.printStackTrace();
		}

		//*** ImpiantoElettrico ***//
		logger.info("Configuring impianto elettrico...");
		elettrodomestici = ImpiantoElettrico.getInstance();
		createAppliances();
	}

	private void createAppliances() {
		//*** Elettrodomestici: 3 per ogni tipo (Indisp Basso, Superfluo Basso, Indisp Medio, ecc) ***//
		logger.info("Creating household appliances...");
		
		// Creazione tramite DomoticDomain (generato dal DSL Domotic)
		DomoticDomain domain = new DomoticDomain();
		elettrodomestici.setElettrodomestici(domain.createAppliances());
		
//		// Elettrodomestici Indispensabili Consumo Basso
//		for(int i = 0 ; i < 3 ; i++) {
//			String edName = "edIB" + i;
//			Elettrodomestico ed = new Elettrodomestico(edName, CatUsage.ESSENTIAL, CatConsumptionEnum.LOW);
//			elettrodomestici.addED(edName, ed);
//		}
//
//		// Elettrodomestici Superflui Consumo Basso
//		for(int i = 0 ; i < 3 ; i++) {
//			String edName = "edSB" + i;
//			Elettrodomestico ed = new Elettrodomestico(edName, CatUsage.OPTIONAL, CatConsumptionEnum.LOW);
//			elettrodomestici.addED(edName, ed);
//		}
//
//		// Elettrodomestici Indispensabili Consumo Medio
//		for(int i = 0 ; i < 3 ; i++) {
//			String edName = "edIM" + i;
//			Elettrodomestico ed = new Elettrodomestico(edName, CatUsage.ESSENTIAL, CatConsumptionEnum.MEDIUM);
//			elettrodomestici.addED(edName, ed);
//		}
//
//		// Elettrodomestici Superflui Consumo Medio
//		for(int i = 0 ; i < 3 ; i++) {
//			String edName = "edSM" + i;
//			Elettrodomestico ed = new Elettrodomestico(edName, CatUsage.OPTIONAL, CatConsumptionEnum.MEDIUM);
//			elettrodomestici.addED(edName, ed);
//		}
//
//		// Elettrodomestici Indispensabili Consumo Alto
//		for(int i = 0 ; i < 3 ; i++) {
//			String edName = "edIA" + i;
//			Elettrodomestico ed = new Elettrodomestico(edName, CatUsage.ESSENTIAL, CatConsumptionEnum.HIGH);
//			elettrodomestici.addED(edName, ed);
//		}
//
//		// Elettrodomestici Superflui Consumo Alto
//		for(int i = 0 ; i < 3 ; i++) {
//			String edName = "edSA" + i;
//			Elettrodomestico ed = new Elettrodomestico(edName, CatUsage.OPTIONAL, CatConsumptionEnum.HIGH);
//			elettrodomestici.addED(edName, ed);
//		}
	}
	
	@Override
	public void doJob(){
		init();
		configure();
//		start();
	}

	@Override
	public void start() {
		/*
		 * 1.	Run conumptionController
		 * 2.	Run edController
		 * 3.	Run receiver
		 */

		// 1.	Run conumeController
		logger.info("Starting consumption controller...");
		consumptionController.start();

		// 2.	Run edController
		logger.info("Starting ED controller...");
		edController.start();
		
		// 3.	Run receiver
		logger.info("Starting receiver...");
		receiver.start();
	}
	
	public void stop() {
		consumptionController.stop();
		edController.setStopThread(true);
		receiver.setStopThread(true);
	}
	
	public static void main(String args[]) throws Exception {
		SysDomotichScontrol system = new SysDomotichScontrol( );
		system.doJob();
		system.start();
	}

}
